package com.travelsupport.ui;


import java.util.List;
import com.travelsupport.controller.DbController;
import com.travelsupport.controller.NetworkController;
import com.travelsupport.myobject.CatalogObj;

import com.travelsupport.ui.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener{

	private static final String TAG = com.travelsupport.common.Common.TAG;
	private Context context = this;
	private String CATALOG_ID = "catalogID";
	
	private DbController  dbController = null;
	private NetworkController networkController = null;
	private CatalogAdapter catalogAdpater;
	
	private Button btnCheck;
	private ListView lvCatalog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/*db=(new DbController(this)).getWritableDatabase();
		
		if(checkNetWorkAvaiable() ) {
			
			//Toast t = new Toast("No network connected");
			Toast toast = Toast.makeText(getApplicationContext(), "Network connected", 10);
			toast.show();
			
			
		}
		else
		{
			Toast toast = Toast.makeText(getApplicationContext(), "No network connected", 10);
			toast.show();
		}*/
		
	/*	Intent i = new Intent(MainActivity.this,DbController.class);
		startActivity(i);*/
		
	//	dbController = new DbController(getApplicationContext());
		//dbController = new DbController(getBaseContext());
		//dbController.selectCatalog();
		
		//dbController = new DbController(this);
		
	//	dbController.onCreate(db);
		
		//dbController.selectCatalog();
		//DbController dbController = new DbController(context);
		initializeGUI();
		//initializeNetwork();
	}
         
	public void settingClick(){
	   
	}
   
	public void catalogItemClick(){
	   
	}
    
    private void initializeGUI(){
    	btnCheck = (Button) findViewById(R.id.btnCheckNetwork);
    	btnCheck.setOnClickListener(this);
    	
    	lvCatalog = (ListView) findViewById(R.id.lv_catalog_list);
    	lvCatalog.setOnItemClickListener(this);
    }
    
    private void initializeNetwork(){
    	if(networkController == null){
    		networkController = new NetworkController(context);
    	}
    	
    	if(networkController.checkNetworkConnected()){
    		//Get dbVersion from local
    		String curDbVersion = "2";
    		//Send request to server
    		networkController.CheckLatestDbVersion(curDbVersion);
    		Toast.makeText(context, "connected", Toast.LENGTH_SHORT).show();
    	} else{
    		Toast.makeText(context, "disConnected", Toast.LENGTH_SHORT).show();
    	}
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btnCheckNetwork:
			//readDb
			/*MySQLiteHelper sqlHelper = new MySQLiteHelper(context);
			SQLiteDatabase db = sqlHelper.getReadableDatabase();*/
			DbController dbController = new DbController(context);
			dbController.readDb();
			dbController.closeDb();
			//loadDb
			
			dbController.openDb();
			List<CatalogObj> objects = dbController.selectAllCatalog();
			Log.i(TAG, "length:" + objects.size());
			catalogAdpater = new CatalogAdapter(context, R.id.lv_catalog_list, objects);
			lvCatalog.setAdapter(catalogAdpater);
			dbController.closeDb();
			//initializeNetwork();
			break;
			default:
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
		TextView tvName = (TextView)childView.findViewById(R.id.tv_catalog_name);
		long catalogID = (Long) tvName.getTag();
		Log.i(TAG, "valut of textview: " + catalogID);
		
		Intent intent = new Intent(context, PlacesActivity.class);
		intent.putExtra(CATALOG_ID, catalogID);
		startActivity(intent);
	}
	
	public void Currency(View view) {
		DbController dbController = new DbController(context);
		dbController.readDb();
		dbController.closeDb();
		startActivity(new Intent(MainActivity.this, CurrencyActivity.class));
	}
	
}
